<?php

namespace App\Models\Goods;

use App\Common\Constant;
use App\Models\BaseModel;

/**
 * 商品素材文件绑定模型类
 *
 * @author WangBin
 * @date 2021-09-24
 * @package App\Models\Goods
 */
class FileBind extends BaseModel
{
    protected $table = 'b2c_goods_file_bind';
    public $timestamps = false;

    /**
     * 保存绑定值
     *
     * @param int $entityId
     * @param int $fileId
     * @param string $bindType
     * @param string $fileType
     * @return mixed
     */
    public static function saveData(int $entityId, int $fileId, string $bindType, string $fileType = Constant::FILE_UPLOAD_TYPE_IMG)
    {
        $fileBind = new FileBind();
        $fileBind->entity_id = $entityId;
        $fileBind->file_id = $fileId;
        $fileBind->entity_type = $bindType;
        $fileBind->file_type = $fileType;
        return $fileBind->save();
    }

    /**
     * 根据商品素材绑定数据
     *
     * @param int $entityId
     * @param string $bindType
     * @param string $fileType
     * @return mixed
     */
    public static function remove(int $entityId, string $bindType = '', string $fileType = Constant::FILE_UPLOAD_TYPE_IMG)
    {
        $fileBind = FileBind::query()->where('entity_id', $entityId);
        if(!empty($bindType)) {
            $fileBind->where('entity_type', $bindType);
        }
        return $fileBind->delete();
    }

    /**
     * 获取商品的素材绑定信息
     *
     * @param int $entityId
     * @param string $bindType
     * @param string $fileType
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getFileBind(int $entityId, string $bindType, string $fileType)
    {
        return FileBind::query()->select(['file_id'])->where('entity_id', $entityId)
            ->where('entity_type', $bindType)
            ->where('file_type', $fileType)
            ->get();
    }
}
